package team.ifgroup.tss.influxdb;

import org.influxdb.InfluxDB;
import org.influxdb.dto.QueryResult;
import team.ifgroup.tss.utils.Csv;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liangyue on 2017/11/26.
 */
public class InfluxdbTest {
    /**
     * 数据库名称
     */
    private static final String database        = "influxdb-database";

    /**
     * 数据报存策略
     */
    private static String       retentionPolicy = "default";

    public static void main(String[] args) {

        InfluxDB influxDB = new InfluxdbBuilder("http://120.78.153.222:8086", "user", "pass").build();

        InfluxdbService service = new InfluxdbService(database, retentionPolicy, influxDB);

        // 创建数据库
        service.createDatabase();

        // 创建数据保存策略
        service.createRetentionPolicy("30d", 1);

        // 插入数据
        Map<String, String> tags = new HashMap<String, String>();
//        tags.put("methodName", "getName");

        String filePath = "/Users/babara/IdeaProjects/TSS/Dataset/SITS1M_fold1_TEST.csv";
        List<String[]> list = Csv.read(filePath);
        for (String[] per : list){
            Map<String, Object> fields = new HashMap<String, Object>();
            for (int i = 0; i < per.length; i ++){
                fields.put(String.valueOf(i), per[i]);
            }
            service.insert("SITS1M_fold1_TEST", tags, fields);
        }


        // 查询数据
        QueryResult queryResult = service.query("select * from \"SITS1M_fold1_TEST\" limit 10");
        List<QueryResult.Result> results = queryResult.getResults();
        if (!results.isEmpty()) {
            for (QueryResult.Result result : results) {
                System.out.println(result.toString());
            }
        }

    }

}
